#ifndef __MINIUTIL_FRAME_THREADSERVICE_H__
#define __MINIUTIL_FRAME_THREADSERVICE_H__

#include "miniutil/thread.h"

/*
	working thread call
	before run
	...
	new thread..
	after run
	while(1)
		working task
		break;
	before quit	
	...
	after quit
		
		
*/
namespace miniutil { namespace frame {
	
class ThreadService
{
public:
	ThreadService(int sleepms = 10);
	int Start();
	int Stop();
    bool IsRunning(){return mbrunning;}

	time_t mstarttm;
	time_t mstoptm;
protected:
	virtual int BeforeStart(){return 0;}
	virtual int AfterStart(){return 0;}
	virtual int BeforeStop(){return 0;}
	virtual int AfterStop(){return 0;}
	virtual int WorkingTask(){return 0;}
protected:

#ifdef WIN32
	static int WorkingThread(void *para);
	HANDLE workerhandle;
#else
	static void* WorkingThread(void *para);
#endif
    int InnerRun();
	int InnerRunException();

	int exitflg;
	bool mbrunning;
	miniutil::mutex mservice_mtx;
	int msleeptime;
};

	
} }	

#endif
